// fix_protobuf.dart
import 'dart:io';

void main() {
  final file = File('douyin.pb.dart');
  if (!file.existsSync()) {
    print('File not found: ${file.path}');
    return;
  }

  String content = file.readAsStringSync();
  
  // 替换方法名 - 使用字符串而不是变量
  content = content.replaceAll('\$_setField', 'setField');
  content = content.replaceAll('\$_clearField', 'clearField');
  content = content.replaceAll('\$_getN', 'getField');
  content = content.replaceAll('\$_ensure', 'ensureField');
  content = content.replaceAll('\$_getSZ', 'getString');
  content = content.replaceAll('\$_setString', 'setString');
  content = content.replaceAll('\$_getI64', 'getInt64');
  content = content.replaceAll('\$_setInt64', 'setInt64');
  content = content.replaceAll('\$_getBF', 'getBool');
  content = content.replaceAll('\$_setBool', 'setBool');
  content = content.replaceAll('\$_getIZ', 'getInt32');
  content = content.replaceAll('\$_setUnsignedInt32', 'setInt32');
  content = content.replaceAll('\$_setSignedInt32', 'setInt32');
  content = content.replaceAll('\$_getList', 'getList');
  
  // BuilderInfo 方法
  content = content.replaceAllMapped(RegExp(r'\.aI\((\d+),'), (match) {
    return '.aInt32(${match.group(1)},';
  });
  
  content = content.replaceAllMapped(RegExp(r'\.pPM<([^>]+)>\((\d+),'), (match) {
    return '.pc<${match.group(1)}>(${match.group(2)},';
  });
  
  // 修复 PbList 类型问题 - 简单的替换
  content = content.replaceAll('PbList<dynamic>', 'PbList<Image>');
  
  file.writeAsStringSync(content);
  print('Fixed protobuf file: ${file.path}');
  print('Please run: flutter clean && flutter pub get');
}